<template>
  <div>
    <el-dialog
      :title="obj.vAdd ? '分类添加' : '分类编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="图片名">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="fileBox1">
            <h2>+</h2>
            <input type="file" @change="changeImg1" v-if="obj.isTrue" />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch
          >{{ form.status }}
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button @click="add()" v-if="obj.vAdd" type="primary"
          >添加</el-button
        >
        <el-button v-else type="primary" @click="update()">编辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { banneradd, bannerinfo, banneredit } from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      //存放图片地址（回显图片）
      imgUrl: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
    };
  },

  mounted() {},

  methods: {
    ...mapActions({
      asyncChangeList: "banner/asyncChangeList",
    }),
    changeImg1(e) {
      //图片回显
      let file = e.target.files[0];
      //限制图片
      console.log(file);
      if (file.size > 5024 * 555) {
        warningAlert("图片大小超出限制");
        return;
      }
      //  let imgType = [
      //         "image/jpeg",
      //         "image/jpg",
      //         "image/png",
      //         "image/gif",
      //         "image/webp",
      //       ];
      // if(imgType.includes(file.type)){
      //    warningAlert("格式不符合")
      //   return
      // }

      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    //清空
    empty() {
      this.form = {
        title: "",
        img: "",
        status: 1,
      };
      this.imgUrl = "";
      this.obj.isTrue = false;
    },
    //取消
    cancel() {
      this.empty();
    },
    //添加
    add() {
      // console.log(this.form);
      banneradd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.asyncChangeList();
        } else {
          warningAlert(res.data.msg);
        }
      });
      this.empty();
    },
    //获取详情信息
    getOne(id) {
      bannerinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.imgUrl = this.$pre + this.form.img;
        this.form.id = id;
      });
    },
    //编辑
    update() {
      banneredit(this.form).then((res) => {
        if (res.data.code == 200) {
          this.empty();
          this.asyncChangeList();
          successAlert(res.data.msg);
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({}),
  },
};
</script>

<style  scoped>
/* 原生js方式 */
.fileBox1 {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  position: relative;
}
.fileBox1 h2 {
  text-align: center;
  line-height: 200px;
  font-size: 60px;
  margin: 0;
}
.fileBox1 input {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.fileBox1 img {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
}

/* element-ui方式 */
/* .fileBox2{
    width: 200px;
    height: 200px;
    border: 1px dashed #ccc;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 200px;
    height: 200px;
    line-height: 200px;
    text-align: center;
  }
  .avatar {
    width: 200px;
    height: 200px;
    display: block;
  } */
</style>